package ru.budget.mobile.domain.usecase.income

import kotlinx.coroutines.flow.*
import ru.budget.mobile.core.*
import ru.budget.mobile.domain.entities.IncomeDomain
import ru.budget.mobile.domain.repository.IncomeRepository
import ru.budget.mobile.ui.entities.IncomeUI

class GetIncomeListUseCase(
    private val repository: IncomeRepository
) {

    operator fun invoke(): Flow<Response<List<IncomeUI>>> = flow {
        emit(Response.Loading(ResponseStates.LOADING))
        emit(
            repository.incomeList().map {
                it.map(mapper)
            }
        )
    }

    private val mapper = { income: IncomeDomain ->
        IncomeUI(
            id = income.id,
            sum = income.sum,
            datetime = income.datetime
        )
    }
}